


var BattleUnit = cc.Node.extend({

    MOVETO_TIME : 4, // 战斗开始前移动时间 4秒

    side : 0,    // 站位 0:下 1:上
	image : null,
    uiHp : null,    // BattleUnitHp类

    id : 0, 
    type : 0,   // 卡片类型
    cx : 0,     // 格子坐标
    cy : 0,
    hp : 0,
    mhp : 0,

    isAttackCD : true,
    isMoveOver : false,
    isGameOver : false,

    _texture_pug : null, // 履带轨迹贴图

    ctor : function (id, type, side, cx, cy, hp, mhp) {
        this._super();

        /*
        cc.log(' ---- BattleUnit ----')
        cc.log(id)
        cc.log(type)
        cc.log(side)
        cc.log(cx)
        cc.log(cy)
        cc.log(hp)
        cc.log(mhp)
		*/

        this.id = id;
        this.type = type;
        this.cx = cx;
        this.cy = cy;
        this.hp = hp;
        this.mhp = mhp;

    	var file = 'res/tank/'+TB_tank[type].image;
        this.image = cc.Sprite.create(file);
        this.image.setScale(0.6);       //* 调整大小
    	this.addChild(this.image);

        this.setCellPos(cx, cy, side);
        this.setSide(side);

        this.uiHp = new BattleUnitHp(hp, mhp, side);
        this.uiHp.setPositionY(-40);
        this.uiHp.setVisible(false);    // 默认不显示HP
        this.addChild(this.uiHp);

        this.uiAim = new BattleUnitAim();
        this.addChild(this.uiAim);
        this.uiAim.setVisible(false);
	},

    // 设置格子坐标
    setCellPos : function (cx, cy, side) {
        this.setPosition( helper.posCell2Scene(cx, cy, side) );
    },

    setSide : function (side) {
        if (side == 1) {
            this.setScaleY(-1);
        }
        this.side = side;
    },

    moveTo : function (cx, cy) {
        var p = helper.posCell2Scene(cx, cy, this.side);
        this.cx = cx;
        this.cy = cy;
        var move = cc.EaseSineOut.create( cc.MoveTo.create(this.MOVETO_TIME, {x:p.x, y:p.y}) );    // 移动
        var func = cc.CallFunc.create(function(target) { target.onMoveToOver(); }, this);  // 结束回调
        this.runAction(cc.Sequence.create(move,func));
    },

    // MoveTo结束回调
    onMoveToOver : function () {
        //cc.log('onMoveToOver');

        this.showHp(true);

        this.isMoveOver = true;
        
        this.startAttackCD();   // 开始CD
    },

    // 血量改变
    addHp : function (hp) {
        //cc.log('addHp:'+this.hp+'+'+hp+'=>'+(this.hp+hp));
        this.hp += hp;
        this.uiHp.addHp(hp);

        if(this.hp <= 0) {
            // 死亡动画
            this.aniDead();
            this.showHp(false);
        }
    },

    showHp : function (isShow) {
        this.uiHp.setVisible(true);
        this.uiHp.show(isShow);
    },

    isDead : function() {
        return this.hp <= 0;
    },

    onClick : function () {
        //cc.log('onClick:'+this.id);
        if(this.isGameOver) return;

        // 发动攻击
        if(!this.isAttackCD) {
            BattleLayer.onUnitAttack(this);
            
            this.startAttackCD();   // 开始CD

            this.uiAim.stop();
            var atkGrade = this.uiAim.getAttackGrade();

            //cc.log('--------------------')
            //cc.log('atkGrade:'+atkGrade);
            //cc.log('--------------------')

        } else {
            cc.log('on attack cd');
        }
        
    },

    startAttackCD : function () {
        this.isAttackCD = true;

        var time = TB_tank[this.type].atk_cd / 1000;    // 表里填的是毫秒 * 需要计算道具加成
        cc.log('attack cd:'+time)
        this.scheduleOnce(this.endAttackCD, time);  // 启动CD
    },

    // 结束CD, 开始瞄准
    endAttackCD : function () {
        cc.log('attack cd over')
        this.isAttackCD = false;

        // 瞄准CD
        this.uiAim.setVisible(true);
        this.uiAim.run();
    },

    // 攻击CD
    updateCD : function (dt) {
        //cc.log('updateCD');
    },

    // 开始战斗
    onStartBattle : function() {
        this.showHp(false);

        // 从后方移动过来
        var sz = cc.Director.getInstance().getWinSize();
        var y = (this.side == helper.SIDE_UP) ? sz.height+helper.CELL_SIZE+this.cy*helper.CELL_SIZE : -helper.CELL_SIZE-this.cy*helper.CELL_SIZE;
        this.setPositionY(y);
        this.moveTo(this.cx, this.cy);

        // 轨迹
        this.schedule(this._addPug, 0.2);
        this.scheduleOnce(this._stopAddPug, this.MOVETO_TIME);
    },

    // 轨迹
    _addPug : function () {
        //cc.log('_addPug');

        // 使用BatchNode优化
        if(! this._texture_pug) {
            this._texture_pug = cc.SpriteBatchNode.create('res/battle/pug.png', 100);
            this.addChild(this._texture_pug);
        }
        //var sp = cc.Sprite.createWithTexture(this._texture_pug.getTexture());
        
        var sp = cc.Sprite.create('res/battle/pug.png');
        sp.setPosition( this.getPosition() );
        sp.setScale(0.5);
        sp.setOpacity(220);
        BattleLayer.addToGroundLayer(sp);
    },

    _stopAddPug : function () {
        //cc.log('_stopAddPug');
        this.unschedule(this._addPug);
    },

    // 结束战斗 (from BattleLayer.checkGameOver)
    onGameOver : function () {
        this.isGameOver = true;
        this.unschedule(this.endAttackCD);
    },


    onEndBattle : function() {

    },


    // 播放动作 -------------------------
    /*
    aniIdle : function () {

        var _func = function () {
            var time = 1.2;
            var height = 30;
            var up = cc.EaseSineInOut.create( cc.MoveTo.create(time, {x:0, y:height}) );   // 往上
            var down = cc.EaseSineInOut.create( cc.MoveTo.create(time, {x:0, y:0}) );      // 往下
            var repeat = cc.RepeatForever.create(cc.Sequence.create(up, down));     // 永远执行序列
            this.swordImg.runAction(repeat);
        };

        this.aniInit(false, _func, this);
    },*/

    aniAttack : function () {
        var push = cc.MoveTo.create(0.3, cc.p(0,-3));          // 被推后
        var back = cc.EaseSineInOut.create( cc.MoveTo.create(0.5, cc.p(0,0)) ); // 回到原始位置

        this.image.runAction(cc.Sequence.create(push, back));
    },

    aniHurt : function () {
        var red = cc.TintTo.create(0.01, 255,64,64);            // 变色 红
        var white = cc.TintTo.create(0.01, 255,255,255);
        var d = cc.DelayTime.create(0.10);                      // 延时
        var push = cc.MoveTo.create(0.1, cc.p(0,-10));          // 被推后
        var back = cc.EaseSineInOut.create( cc.MoveTo.create(0.5, cc.p(0,0)) ); // 回到原始位置
        var func = cc.CallFunc.create(function(target) {}, this);  // 结束回调
        
        this.image.runAction(cc.Sequence.create(
            push, 
            red, d, white, d,
            red, d, white, d,
            red, d, white, d,
            red, d, white, d, 
            red, d, white, back)
        );
    },

    aniDead : function () {
        var d = cc.DelayTime.create(1);                     // 延时
        var fade = cc.FadeTo.create(1, 0);                  // 渐变
        var func = cc.CallFunc.create(function(target) {}, this);  // 结束回调
        
        this.image.runAction(cc.Sequence.create(d, fade, func));
    },

    aniReady : function () {
        var d = cc.DelayTime.create(1);                     // 延时
        var fade = cc.FadeTo.create(1, 0);                  // 渐变
        var func = cc.CallFunc.create(function(target) {}, this);  // 结束回调
        
        this.image.runAction(cc.Sequence.create(d, fade, func));
    },


});

